package com.xsm.ppsecret.presenter;

import android.util.Base64;

import com.xsm.ppsecret.App;
import com.xsm.ppsecret.config.Authority;
import com.xsm.ppsecret.ui.implview.ILoginActivity;
import com.xsm.ppsecret.util.CryptUtil;
import com.xsm.ppsecret.util.NativeHelper;
import com.xsm.ppsecret.util.ShareManager;

/**
 * Author: Xiasem
 * Email: xiasem@163.com
 * Date: 17-4-14.
 */

public class LoginPresenter extends BasePresenter {

    private ILoginActivity mLoginActivity;

    public LoginPresenter(ILoginActivity loginActivity) {
        mLoginActivity = loginActivity;
    }

    /**
     * 登录密码校验
     * @param password 用户输入密码
     */
    public void unLock(String password) {
        if (password == null || password.isEmpty()) {
            mLoginActivity.unLockError("请输入密码后登录");
            return;
        }
        ShareManager shareManager = ShareManager.getInstance();
        String rootData = shareManager.getPassword();
        byte[] rootBytes = CryptUtil.aesDecryptWithIv(Base64.decode(rootData, Base64.NO_WRAP), NativeHelper.getAesKey(), NativeHelper.getAesIvKey());
        String root = new String(rootBytes);
        if (password.equals(root)) {
            App.getInstance().authority = Authority.ROOT;
            mLoginActivity.unLockOk();
            return;
        }

        String oneData = shareManager.getPasswordOne();
        byte[] oneBytes = CryptUtil.aesDecryptWithIv(Base64.decode(oneData, Base64.NO_WRAP), NativeHelper.getAesKey(), NativeHelper.getAesIvKey());
        String one = new String(oneBytes);
        if (password.equals(one)) {
            App.getInstance().authority = Authority.FIRST_GRADE;
            mLoginActivity.unLockOk();
            return;
        }

        String twoData = shareManager.getPasswordTwo();
        byte[] twoBytes = CryptUtil.aesDecryptWithIv(Base64.decode(twoData, Base64.NO_WRAP), NativeHelper.getAesKey(), NativeHelper.getAesIvKey());
        String two = new String(twoBytes);
        if (password.equals(two)) {
            App.getInstance().authority = Authority.SECOND_GRADE;
            mLoginActivity.unLockOk();
            return;
        }

        mLoginActivity.unLockError("密码错误");

    }

}
